﻿<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages.Administration, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Assembly Name="Kailua, Version=#KAILUA_VERSION_STR, Culture=neutral, PublicKeyToken=86d340ffca32cc5b" %>
<%@ Page Language="C#" MasterPageFile="~/_admin/popup.master" %>

<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="Microsoft.SharePoint.Administration" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="net.windward.utils" %>
<%@ Import Namespace="net.windward.utils.ado" %>
<%@ Import Namespace="net.windward.utils.ado.SqlServer" %>
<%@ Import Namespace="System.Reflection" %>

<script runat="server"> 
    private string mode;
    private Guid web = Guid.Empty;
    private string key = String.Empty;
    
    const string LICENSE_HEADER = "<H3 class='ms-standardheader'>Edit Arrow License Key</H3>";
    const string CONNSTR_HEADER = "<H3 class='ms-standardheader'>Edit Arrow Database Connection String</H3>";

    const string JS_CONFIRM = "return confirmModify('{0}','{1}')";

    const string MSG_EDT = "Please confirm that you want to create the Arrow database";

    const string NA = "(None found)";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        lnkSave1.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_EDT));
        lnkSave2.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_EDT));
        btnSave1.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_EDT));
        btnSave2.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_EDT));
    }
   
    private void Save()
    {
        try
        {
            dbNameErrorLabel.Text = null;

            string dbName = dbNameTextBox.Text;
            if (string.IsNullOrEmpty(dbName))
            {
                lblMsg.Text = "<span style=\"color:red\">*</span> Please enter a Database Name";
                dbNameErrorLabel.Text = "<span style=\"color:red\">*</span> Please enter a Database Name";
                return;
            }
            dbName = dbName.Trim();
            
            WrVendor vendor = new WrSqlServerVendor();
            Assembly assembly = Assembly.Load(new AssemblyName("ArrowWebParts2007, Version=#ARROW_MAJOR_VERSION_STR, Culture=neutral, PublicKeyToken=bface3e8a4e95820"));
            WrCredentials cred;
            
            using (Stream stream = assembly.GetManifestResourceStream("ArrowWebParts.CreateArrowDatabaseAndTables.sql"))
            {
                if (stream == null)
                {
                    lblMsg.Text = "Error loading SQL script.";
                    return;
                }
                
                using (StreamReader reader = new StreamReader(stream))
                {
                    // replace the db name
                    string script = reader.ReadToEnd();
                    script = script.Replace("[Arrow]", string.Format("[{0}]", dbName));
                    byte[] bytes = Encoding.ASCII.GetBytes(script);
                    MemoryStream scriptStream = new MemoryStream(bytes);

                    // run the script
                    using (StreamReader scriptReader = new StreamReader(scriptStream))
                    {
                        if (authRadioButtonList.SelectedValue == "sql")
                        {
                            cred = new WrCredentials(usernameTextBox.Text, passwordTextBox.Text);
                            vendor.RunScript(serverTextBox.Text, "master", scriptReader, cred);
                        }
                        else
                        {
                            cred = new WrCredentials();
                            
                            // workaround for Integrated Security=SSPI error
                            string connStrScript = vendor.ConnectionString(serverTextBox.Text, "master", cred, true);
                            connStrScript = connStrScript.Replace("Integrated Security=SSPI", "Integrated Security=True");

                            vendor.RunScript(connStrScript, scriptReader);
                        }
                    }
                }
            }

            lblMsg.Text = "Database created.  Connection string to database:";
            
            // workaround for Integrates Security=SSPI error
            string connStr = vendor.ConnectionString(serverTextBox.Text, dbName, cred, true);
            connStr = connStr.Replace("Integrated Security=SSPI", "Integrated Security=True");

            connectionStringTextBox.Text = connStr;
            connectionStringTextBox.Visible = true;
        }
        catch(Exception e)
        {
            lblMsg.Text = "Error creating database.  Error message:<br/>   " + e.Message;
            connectionStringTextBox.Text = e.StackTrace;
            connectionStringTextBox.Visible = true;
        }
    }

    protected void Save_Click(object sender, ImageClickEventArgs e)
    {
        Save();
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        Save();
    }
</script>

<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
  <div>
        <table cellpadding="0" cellspacing="0" style="height: 410; width: 100%">
            <tr>
                <td valign="top" style="height: 10%">
                    <table class="ms-toolbar" width="100%" style="padding-left: 3px; padding-right: 5px;"
                        cellspacing="0" cellpadding="2">
                        <tr>
                            <td width="10%">
                                <table cellspacing="1" cellpadding="0" border="0">
                                    <tr>
                                        <td width="0" class="ms-toolbar">
                                            <a href="javascript:__doPostBack(&#39;ctl00$PlaceHolderMain$ProfileSave&#39;,&#39;&#39;)"
                                                title="Create">
                                                <asp:ImageButton ID="btnSave1" AlternateText="Create" ImageUrl="/_layouts/images/save.gif"
                                                    BorderWidth="0" runat="server" OnClick="Save_Click" /></a>
                                        </td>
                                        <td class="" width="100%" nowrap>
                                            <asp:LinkButton ID="lnkSave1" runat="server" CssClass="ms-toolbar" ToolTip="Create"
                                                OnClick="Save_Click">Create</asp:LinkButton>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td class="ms-separator" width="2%">
                                |
                            </td>
                            <td>
                                <table cellspacing="1" cellpadding="0" border="0">
                                    <tr>
                                        <td width="100%" nowrap>
                                            <a class="ms-toolbar" href="javascript:window.close();">Close</a>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td valign="top" style="height: 100%;">
                    <table cellpadding="5" cellspacing="5">
                        <tr>
                            <td>
                                <asp:Label ID="headerLabel" runat="server" Text=""></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <table>
                                    <!-- db name -->
                                    <tr>
                                        <td class="ms-authoringcontrols">
                                            <asp:Label ID="dbNameLabel" runat="server" CssClass="ms-authoringcontrols" Text="Database Name"></asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:TextBox ID="dbNameTextBox" runat="server" Columns="35" MaxLength="260" CssClass="ms-input"></asp:TextBox>
                                        </td>
                                        <td>
                                            <asp:Label ID="dbNameErrorLabel" runat="server" CssClass="ms-descriptiontext ms-inputformdescription"></asp:Label>
                                        </td>
                                    </tr>
                                    <!-- server -->
                                    <tr>
                                        <td class="ms-authoringcontrols">
                                            <asp:Label ID="serverLabel" runat="server" CssClass="ms-authoringcontrols" Text="Database Server"></asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:TextBox ID="serverTextBox" runat="server" Columns="35" MaxLength="260" CssClass="ms-input"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <!-- authentication type radio button-->
                                    <tr>
                                        <td>
                                            <asp:Label ID="credLabel" runat="server" CssClass="ms-authoringcontrols" Text="Database Credentials"></asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:RadioButtonList ID="authRadioButtonList" runat="server" CssClass="ms-descriptiontext">
                                                <asp:ListItem Text="Windows Authentication" Value="win" Selected="True" />
                                                <asp:ListItem Text="SQL Authentication" Value="sql" />
                                            </asp:RadioButtonList>
                                        </td>
                                    </tr>
                                    <!-- account name -->
                                    <tr>
                                        <td class="ms-authoringcontrols">
                                            <asp:Label ID="usernameLabel" runat="server" CssClass="ms-authoringcontrols" Text="Account Name"></asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:TextBox ID="usernameTextBox" runat="server" Columns="35" MaxLength="260" CssClass="ms-input"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <!-- password -->
                                    <tr>
                                        <td class="ms-authoringcontrols">
                                            <asp:Label ID="passwordLabel" runat="server" CssClass="ms-authoringcontrols" Text="Password"></asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:TextBox ID="passwordTextBox" runat="server" Columns="35" MaxLength="260" CssClass="ms-input" TextMode="Password"></asp:TextBox>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label runat="server" ID="lblMsg" Width="380px" class="ms-descriptiontext ms-inputformdescription" ></asp:Label>
                            </td>
                        </tr>
                        <tr> <!-- For connection string -->
                            <td>
                                <asp:TextBox ID="connectionStringTextBox" runat="server" Width="380px" TextMode="MultiLine" Text="" Visible="false" ReadOnly="true"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>           
            <tr>
                <td valign="bottom" style="height: 10%">
                    <table id="Table2" class="ms-toolbar" width="100%" style="padding-left: 3px; padding-right: 5px;"
                        cellspacing="0" cellpadding="2">
                        <tr>
                            <td width="10%">
                                <table cellspacing="1" cellpadding="0" border="0">
                                    <tr>
                                        <td width="0" class="ms-toolbar">
                                            <a href="javascript:__doPostBack(&#39;ctl00$PlaceHolderMain$ProfileSave&#39;,&#39;&#39;)"
                                                title="Create">
                                                <asp:ImageButton ID="btnSave2" OnClick="Save_Click" AlternateText="Create"
                                                    ImageUrl="/_layouts/images/save.gif" BorderWidth="0" runat="server" /></a>
                                        </td>
                                        <td width="100%" nowrap>
                                            <asp:LinkButton ID="lnkSave2" OnClick="Save_Click" CssClass="ms-toolbar"
                                                runat="server" ToolTip="Create">Create</asp:LinkButton>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td class="ms-separator" width="1%">
                                |
                            </td>
                            <td>
                                <table cellspacing="1" cellpadding="0" border="0">
                                    <tr>
                                        <td width="100%" nowrap>
                                            <a class="ms-toolbar" href="javascript:window.close();">Close</a>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
    

    <script language="javascript">
        function confirmModify(key, question) {
            var question = question + key;
            var answer = confirm(question);
            if (answer) {               
                return true;
            }
            else if (!answer) {
                return false;
            }

        }
       
    </script>
</asp:Content>

<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    Create Arrow Database
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
    Create Arrow Database
</asp:Content>
